//
//  NavigatorViewController.m
//  NewApp
//
//  Created by 健 吕 on 12-6-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NavigatorViewController.h"
#import "AppDelegate.h"
#import "SystemInlineViewController.h"
#import "LastedMessagesListViewController.h"
#import "DigitAlbumViewController.h"
#import "MapSeachViewController.h"
#import "DigitNoteViewController.h"
#import "ContactUsViewController.h"
#import "RegistViewController.h"
#import "MemberShipViewController.h"
#import "InquiryPriceViewController.h"
#import "OnlineShoppingViewController.h"
#import "ProductListViewController.h"
#import "MemberShipViewController.h"
#import "LastedMessageViewController.h"

@interface NavigatorViewController ()

/**
 *  Build UI elements
 */
- (void) buildUerInterface;

/**
 *  Function button pressed
 */
- (void) functionButtonPressed:(id) sender;


@end

@implementation NavigatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
	self.titleLabel.text = @"導覽頁";
    [self buildUerInterface];
    
    //Send request
    [self.engine navigatorInfos];
    
}


- (void) buildUerInterface {
    
    NSArray *imageArray = [NSArray arrayWithObjects:@"first_page_image", @"guide_image", @"system_image",  @"latest_image",
                           @"album_image",@"note_image", @"price_image", @"online_image", @"member_image",@"contact_image",@"registration_image",@"map_image", nil];
    NSArray *titleArray = [NSArray arrayWithObjects:@"首 頁", @"導覽頁", @"系統內頁", @"最新消息", @"電子相簿",@"電子型錄", @"詢價系統", @"線上購物", @"會員專區",@"聯繫我們",@"線上註冊",@"地圖搜尋", nil]; 
    
    
    CGSize size = CGSizeMake(320.f, 300.f);
    
    CGFloat cellWidth   = size.width / 4;
    CGFloat cellHeight  = size.height / 3;
    NSInteger iconWidth = 60.f;
    NSInteger iconHeight= 60.f;
    CGFloat startX      = (cellWidth - iconWidth) / 2;
    CGFloat startY      = (cellHeight - iconHeight) / 2 + 25.f;
    
    UIButton *btnTemp = nil;
    UILabel *lblTemp = nil;
    
    for (int i = 0; i < 3; i ++) {
        for (int k = 0; k < 4; k ++) {
            
            int index = i * 4 + k;
            if (index >= imageArray.count)
                break;
            
            CGFloat x = k * cellWidth + startX;
            CGFloat y = i * cellHeight+ startY;
            CGRect rect = CGRectMake(x, y, iconWidth, iconHeight);
            
            btnTemp = [UIButton buttonWithType:UIButtonTypeCustom];
            NSString *imageName = [imageArray objectAtIndex:index];
            [btnTemp setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", imageName]] forState:UIControlStateNormal];
            [btnTemp addTarget:self action:@selector(functionButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            btnTemp.tag = index;
            btnTemp.frame = rect;
            [self.view addSubview:btnTemp];
            
            lblTemp = [[UILabel alloc] initWithFrame:CGRectMake(btnTemp.frame.origin.x, btnTemp.frame.origin.y + 55.0f,
                                                                btnTemp.frame.size.width, 32.0f)];
            lblTemp.backgroundColor = [UIColor clearColor];
            lblTemp.textAlignment = UITextAlignmentCenter;
            lblTemp.textColor = [UIColor whiteColor];
            lblTemp.font = [UIFont systemFontOfSize:12.f];
            lblTemp.text = [titleArray objectAtIndex:index];
            [self.view addSubview:lblTemp];
            [lblTemp release];
        }
    }
}

- (void) functionButtonPressed:(id) sender {
    
    UIButton *button = (UIButton *) sender; 
    switch (button.tag) {
        case BTN_TAG_HOME_PAGE:
            [AppDelegate theAppDelegate].tabBarController.selectedIndex = TABBAR_ITEM_HOME;
            
            break;
        case BTN_TAG_SYSTEM_PAGE: {
            SystemInlineViewController *viewCtrl = [[SystemInlineViewController alloc] init];
            [self.navigationController pushViewController:viewCtrl animated:YES];
            [viewCtrl release];
        }
            break;
        case BTN_TAG_LASTED_NEWS:{
            //[AppDelegate theAppDelegate].tabBarController.selectedIndex = TABBAR_ITEM_PRODUCT_LIST;
            LastedMessageViewController *viewCtrl = [[LastedMessageViewController alloc] init];
            [self.navigationController pushViewController:viewCtrl animated:YES];
            //[viewCtrl release];
            break;
        }
        case BTN_TAG_ABLUM: {
            DigitAlbumViewController *viewCtrl = [[DigitAlbumViewController alloc] init];
            [self.navigationController pushViewController:viewCtrl animated:YES];
            [viewCtrl release];
            break;
        }
        case BTN_TAG_MAP_SEARCH:{
            MapSeachViewController *vctrl = [[MapSeachViewController alloc] init];
            [self.navigationController pushViewController:vctrl animated:YES];
            [vctrl release];
            break;
        }
        case BTN_TAG_NOTE:{
            DigitNoteViewController *viewCtrl = [[DigitNoteViewController alloc] init];
            [self.navigationController pushViewController:viewCtrl animated:YES];
            [viewCtrl release];
            break;
        }
        case BTN_TAG_CONTACT_US:{
            ContactUsViewController *vctrl = [[ContactUsViewController alloc] init];
            [self.navigationController pushViewController:vctrl animated:YES];
            [vctrl release];
            break;
        }
        case BTN_TAG_ONLINE_REG:{
            RegistViewController *vctrl = [[RegistViewController alloc] init];
            [self.navigationController pushViewController:vctrl animated:YES];
            [vctrl release];
            break;
        }
        case BTN_TAG_PRICE_QUERY:{
            InquiryPriceViewController *vctrl = [[InquiryPriceViewController alloc] init];
            [self.navigationController pushViewController:vctrl animated:YES];
            [vctrl release];
            break;
        }
        case BTN_TAG_MEMBER_AREA:{
            [AppDelegate theAppDelegate].tabBarController.selectedIndex = TABBAR_ITEM_MEMBER;
//            MemberShipViewController *vctrl = [[MemberShipViewController alloc] init];
//            [self.navigationController pushViewController:vctrl animated:YES];
//            [vctrl release];
            break;
        }
        case BTN_TAG_ONLINE_SHOPPING:{
            OnlineShoppingViewController *vctrl = [[OnlineShoppingViewController alloc] init];
            [self.navigationController pushViewController:vctrl animated:YES];
            [vctrl release];
            break;
        }

        default:
            break;
    }
}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dic = (NSDictionary *) object;
        [dic retain];
        
        NSString *title = [dic objectForKey:@"top_title"];
        if (title != nil && title.length > 0)
            self.titleLabel.text = title;
    }
}

@end
